clear all
#delimit ;
version 11.2;
set mem 350m;
set matsize 800;
*set maxvar 32767;
set more off;
cap log close;


cd "H:\Mi unidad\Grant_new\EFF";

foreach var in housing /*busloan credcard restbis*//**/{;
foreach y in 2017{;
use sample_eff.dta, clear;
di "YEAR " "`y'";
keep if year==`y';


cap drop facine3_imp1 facine3_imp2 facine3_imp3 facine3_imp4;
rename facine3_imp5 facine3;
svyset [pw=facine3];



foreach x in 1 2 3 4 5{;
replace hhincome_imp`x'*=hhincome_imp`x'*10;
replace nwealth_imp`x'*=nwealth_imp`x'/1000;

drop if oth_inact_imp`x'==1 | disable_imp`x'==1 | student_imp`x'==1 | housew_imp`x'==1;
keep if age_imp`x'>=3 & age_imp`x' <=7.5;
drop if madult_imp`x'==1;

********* Nominal terms *****;
replace hhincome_imp`x'=hhincome_imp`x'*acpi_imp1/100;
replace nwealth_imp`x'=nwealth_imp`x'*acpi_imp1/100;
*****************************;

replace age_imp`x'=age_imp`x'*10;
replace bhealth_imp`x'=bhealth_imp`x'*100;
replace univ_imp`x'=univ_imp`x'*100;
replace unemployed_imp`x'=unemployed_imp`x'*100;
replace retiree_imp`x'=retiree_imp`x'*100;
replace selfemployed_imp`x'=selfemployed_imp`x'*100;
replace sex_imp`x'=sex_imp`x'*100;
replace couplewith_imp`x'=couplewith_imp`x'*100;
replace couplewithout_imp`x'=couplewithout_imp`x'*100;
replace singlewith_imp`x'=singlewith_imp`x'*100;
replace risklot_imp`x'=risklot_imp`x'*100;
replace riskfair_imp`x'=riskfair_imp`x'*100;
replace risksome_imp`x'=risksome_imp`x'*100;
replace anydebt_imp`x'=anydebt_imp`x'*100;
replace int_housing_imp`x'=int_housing_imp`x'/100 if year==2002;
replace int_restbis_imp`x'=int_restbis_imp`x'/100 if year==2002;
replace int_busloan_imp`x'=int_busloan_imp`x'/100 if year==2002;
replace int_housing_imp`x'=. if int_housing_imp`x'>25;
replace int_restbis_imp`x'=. if int_restbis_imp`x'>30;
replace ration2_imp`x'=ration2_imp`x'*100;

svy: mean age_imp`x' bhealth_imp`x' univ_imp`x' unemployed_imp`x' retiree_imp`x' selfemployed_imp`x' 
	sex_imp`x' couplewith_imp`x' couplewithout_imp`x' singlewith_imp`x' hhincome_imp`x' nwealth_imp`x'
	risklot_imp`x' riskfair_imp`x' risksome_imp`x' riskaver_imp`x' anydebt_imp`x' ration2_imp`x';
	
matrix b_imp`x'=e(b);
matrix v_imp`x'=e(V);

svy: mean int_housing_imp`x';
matrix b_housing_imp`x'=e(b);
matrix v_housing_imp`x'=e(V);

svy: mean int_busloan_imp`x';
matrix b_busloan_imp`x'=e(b);
matrix v_busloan_imp`x'=e(V);

*svy: mean int_credcard_imp`x';
*matrix b_credcard_imp`x'=e(b);
*matrix v_credcard_imp`x'=e(V);

svy: mean int_restbis_imp`x';
matrix b_restbis_imp`x'=e(b);
matrix v_restbis_imp`x'=e(V);

di "Year "`y';

scalar mean_age_imp`x'=b_imp`x'[1,1];
scalar var_age_imp`x'=v_imp`x'[1,1];

scalar mean_bhealth_imp`x'=b_imp`x'[1,2];
scalar var_bhealth_imp`x'=v_imp`x'[2,2];

scalar mean_univ_imp`x'=b_imp`x'[1,3];
scalar var_univ_imp`x'=v_imp`x'[3,3];

scalar mean_unemployed_imp`x'=b_imp`x'[1,4];
scalar var_unemployed_imp`x'=v_imp`x'[4,4];

scalar mean_retiree_imp`x'=b_imp`x'[1,5];
scalar var_retiree_imp`x'=v_imp`x'[5,5];

scalar mean_selfemployed_imp`x'=b_imp`x'[1,6];
scalar var_selfemployed_imp`x'=v_imp`x'[6,6];

scalar mean_sex_imp`x'=b_imp`x'[1,7];
scalar var_sex_imp`x'=v_imp`x'[7,7];

scalar mean_couplewith_imp`x'=b_imp`x'[1,8];
scalar var_couplewith_imp`x'=v_imp`x'[8,8];

scalar mean_couplewithout_imp`x'=b_imp`x'[1,9];
scalar var_couplewithout_imp`x'=v_imp`x'[9,9];

scalar mean_singlewith_imp`x'=b_imp`x'[1,10];
scalar var_singlewith_imp`x'=v_imp`x'[10,10];

scalar mean_hhincome_imp`x'=b_imp`x'[1,11];
scalar var_hhincome_imp`x'=v_imp`x'[11,11];

scalar mean_nwealth_imp`x'=b_imp`x'[1,12];
scalar var_nwealth_imp`x'=v_imp`x'[12,12];

scalar mean_risklot_imp`x'=b_imp`x'[1,13];
scalar var_risklot_imp`x'=v_imp`x'[13,13];

scalar mean_riskfair_imp`x'=b_imp`x'[1,14];
scalar var_riskfair_imp`x'=v_imp`x'[14,14];

scalar mean_risksome_imp`x'=b_imp`x'[1,15];
scalar var_risksome_imp`x'=v_imp`x'[15,15];

scalar mean_riskaver_imp`x'=b_imp`x'[1,16];
scalar var_riskaver_imp`x'=v_imp`x'[16,16];

scalar mean_anydebt_imp`x'=b_imp`x'[1,17];
scalar var_anydebt_imp`x'=v_imp`x'[17,17];

scalar mean_ration2_imp`x'=b_imp`x'[1,18];
scalar var_ration2_imp`x'=v_imp`x'[18,18];

scalar mean_int_housing_imp`x'=b_housing_imp`x'[1,1];
scalar var_int_housing_imp`x'=v_housing_imp`x'[1,1];

scalar mean_int_busloan_imp`x'=b_busloan_imp`x'[1,1];
scalar var_int_busloan_imp`x'=v_busloan_imp`x'[1,1];

*scalar mean_int_credcard_imp`x'=b_credcard_imp`x'[1,1];
*scalar var_int_credcard_imp`x'=v_credcard_imp`x'[1,1];

scalar mean_int_restbis_imp`x'=b_restbis_imp`x'[1,1];
scalar var_int_restbis_imp`x'=v_restbis_imp`x'[1,1];
};

foreach var in age bhealth univ unemployed retiree selfemployed sex couplewith couplewithout singlewith hhincome nwealth risklot riskfair risksome riskaver anydebt ration2 
	int_housing	int_busloan /*int_credcard*/ int_restbis{;
scalar summean_`var'0 = 0;
scalar sumvar_`var'0 = 0;

forvalues i = 1(1)5 {;
local j = `i' - 1;
scalar summean_`var'`i' = scalar(summean_`var'`j') + scalar(mean_`var'_imp`i');
scalar sumvar_`var'`i' = scalar(sumvar_`var'`j') + scalar(var_`var'_imp`i');
} ;

scalar mean_`var' = (1/5)*scalar(summean_`var'5);
scalar u_`var' = (1/5)*scalar(sumvar_`var'5);
};


foreach x in 1 2 3 4 5{;


scalar difmeansq_age_imp`x'=(b_imp`x'[1,1]-scalar(mean_age))^2;
scalar difmeansq_bhealth_imp`x'=(b_imp`x'[1,2]-scalar(mean_bhealth))^2;
scalar difmeansq_univ_imp`x'=(b_imp`x'[1,3]-scalar(mean_univ))^2;
scalar difmeansq_unemployed_imp`x'=(b_imp`x'[1,4]-scalar(mean_unemployed))^2;
scalar difmeansq_retiree_imp`x'=(b_imp`x'[1,5]-scalar(mean_retiree))^2;
scalar difmeansq_selfemployed_imp`x'=(b_imp`x'[1,6]-scalar(mean_selfemployed))^2;
scalar difmeansq_sex_imp`x'=(b_imp`x'[1,7]-scalar(mean_sex))^2;
scalar difmeansq_couplewith_imp`x'=(b_imp`x'[1,8]-scalar(mean_couplewith))^2;
scalar difmeansq_couplewithout_imp`x'=(b_imp`x'[1,9]-scalar(mean_couplewithout))^2;
scalar difmeansq_singlewith_imp`x'=(b_imp`x'[1,10]-scalar(mean_singlewith))^2;
scalar difmeansq_hhincome_imp`x'=(b_imp`x'[1,11]-scalar(mean_hhincome))^2;
scalar difmeansq_nwealth_imp`x'=(b_imp`x'[1,12]-scalar(mean_nwealth))^2;
scalar difmeansq_risklot_imp`x'=(b_imp`x'[1,13]-scalar(mean_risklot))^2;
scalar difmeansq_riskfair_imp`x'=(b_imp`x'[1,14]-scalar(mean_riskfair))^2;
scalar difmeansq_risksome_imp`x'=(b_imp`x'[1,15]-scalar(mean_risksome))^2;
scalar difmeansq_riskaver_imp`x'=(b_imp`x'[1,16]-scalar(mean_riskaver))^2;
scalar difmeansq_anydebt_imp`x'=(b_imp`x'[1,17]-scalar(mean_anydebt))^2;
scalar difmeansq_ration2_imp`x'=(b_imp`x'[1,18]-scalar(mean_ration2))^2;
scalar difmeansq_int_housing_imp`x'=(b_housing_imp`x'[1,1]-scalar(mean_int_housing))^2;
scalar difmeansq_int_busloan_imp`x'=(b_busloan_imp`x'[1,1]-scalar(mean_int_busloan))^2;
*scalar difmeansq_int_credcard_imp`x'=(b_imp`x'[1,1]-scalar(mean_int_credcard))^2;
scalar difmeansq_int_restbis_imp`x'=(b_restbis_imp`x'[1,1]-scalar(mean_int_restbis))^2;
};

foreach var in hhincome age bhealth univ unemployed retiree selfemployed sex couplewith couplewithout singlewith hhincome nwealth risklot riskfair risksome riskaver anydebt ration2
		int_housing int_busloan /*int_credcard*/ int_restbis{;

scalar sumdifmeansq_`var'0 = 0;

forvalues i = 1(1)5 {;
local j = `i' - 1;
scalar sumdifmeansq_`var'`i' = sumdifmeansq_`var'`j' + difmeansq_`var'_imp`i';
} ;

scalar b_`var'=(1/4)*sumdifmeansq_`var'5;
scalar t_`var'=scalar(u_`var')+(6/5)*scalar(b_`var');
scalar sqt_`var'=scalar(t_`var')^(1/2);
};

foreach var in age bhealth univ unemployed retiree selfemployed sex couplewith couplewithout singlewith hhincome nwealth risklot riskfair risksome riskaver anydebt ration2 int_housing
		int_busloan /*int_credcard*/ int_restbis {;

di "`var'";
di %3.1f scalar(mean_`var');
di %3.2f scalar(sqt_`var');
di "";


};	



};
};
